package com.xihuanjun.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
import com.auth0.jwt.interfaces.Verification;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: 江溪浣
 * @createDate: 2022/9/23 17:28
 * @description:
 */
@Data
@ConfigurationProperties(prefix = "xihuanjun.jwt")
public class JWTUtils {
    private  String tokenSalt ="123456";

    private int tokenExpiresMin =1440;
    private int refTokenExpiresMin=2880;


    /**
     * 根据数据生成token
     * @param map
     * @param expiresDays
     * @return
     */
    public  String getToken(Map<String,String> map,Integer expiresDays){
        JWTCreator.Builder builder= JWT.create();
        map.forEach((k,v)->{
            builder.withClaim(k,v);
        });
        Calendar instance=Calendar.getInstance();
        instance.add(Calendar.MINUTE,expiresDays);
        builder.withExpiresAt(instance.getTime());
        return builder.sign(Algorithm.HMAC256(tokenSalt));
    }

    /**
     * 根据用户基本信息生成双token
     * @param map 要存入token的用户基本信息，必须有一个key：userid
     * @return token和refToken，
     */
    public  Map<String,String> generateUserToken(Map<String, String> map){
        String token1=getToken(map, tokenExpiresMin);//TOKEN_MIN是有效期

        Map<String, String> map2 = new HashMap<>();
        map2.put("userid", map.get("userid").toString());
        String token2=getToken(map2,refTokenExpiresMin);//refToken有效期通常是token有效期的两倍

        Map<String,String> result=new HashMap<>();
        result.put("token",token1);
        result.put("refToken",token2);
        return result;
    }

    /**
     * 验证token有效性
     */
    public  DecodedJWT verify(String token){
        Algorithm a = Algorithm.HMAC256(tokenSalt);
        Verification verification=JWT.require(a);
        JWTVerifier build = verification.build();
        if(build==null || token==null){

        }
        return build.verify(token);
    }

    public  DecodedJWT decodeToken(String token){
        return JWT.require(Algorithm.HMAC256(tokenSalt)).build().verify(token);
    }

    public  Integer getRefreshUseId(HttpServletRequest request){
        String token = request.getHeader("refToken");
        DecodedJWT jwt = verify(token);
        Integer userid = Integer.parseInt(jwt.getClaim("userid").asString());
        return  userid;
    }

    /**
     * 根据request获取里面token存储的某个值
     * @param request
     * @return
     */
    public  String getTokenValue(HttpServletRequest request,String key){
        String token = request.getHeader("token");
        DecodedJWT jwt = verify(token);
        return jwt.getClaim(key).asString();
    }

    public  long getUserId(HttpServletRequest request){
        String token = request.getHeader("token");
        DecodedJWT jwt = verify(token);
        return Long.parseLong( jwt.getClaim("userid").asString());
    }

}
